﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace YArchitech.HVAC
{
	internal class XML
	{
		public static void creat(string path, string rootNode)
		{
			if (!File.Exists(path))
			{
				XmlDocument xmlDocument = new XmlDocument();
				XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
				xmlDocument.AppendChild(newChild);
				XmlElement xmlElement = xmlDocument.CreateElement(rootNode);
				xmlDocument.AppendChild(xmlElement);
				XmlNode xmlNode = xmlDocument.CreateElement("styleNode");
				(xmlNode as XmlElement).SetAttribute("name", "当前样式");
				xmlElement.AppendChild(xmlNode);
				XmlNode xmlNode2 = xmlDocument.CreateElement("System");
				(xmlNode2 as XmlElement).SetAttribute("name", "空调风系统");
				xmlNode.AppendChild(xmlNode2);
				XmlNode xmlNode3 = xmlDocument.CreateElement("System");
				(xmlNode3 as XmlElement).SetAttribute("name", "空调水系统");
				xmlNode.AppendChild(xmlNode3);
				XmlNode xmlNode4 = xmlDocument.CreateElement("System");
				(xmlNode4 as XmlElement).SetAttribute("name", "消防系统");
				xmlNode.AppendChild(xmlNode4);
				XmlNode xmlNode5 = xmlDocument.CreateElement("System");
				(xmlNode5 as XmlElement).SetAttribute("name", "给排水系统");
				xmlNode.AppendChild(xmlNode5);
				XmlNode xmlNode6 = xmlDocument.CreateElement("System");
				(xmlNode6 as XmlElement).SetAttribute("name", "采暖系统");
				xmlNode.AppendChild(xmlNode6);
				XmlNode xmlNode7 = xmlDocument.CreateElement("System");
				(xmlNode7 as XmlElement).SetAttribute("name", "其他");
				xmlNode.AppendChild(xmlNode7);
				xmlDocument.Save(path);
			}
		}

		public static List<bigStruct> readBig(string path, string rootNodeName, int startupType = 0)
		{
			List<bigStruct> list = new List<bigStruct>();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			foreach (object obj in xmlDocument.SelectSingleNode(rootNodeName).ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				bigStruct bigStruct = new bigStruct();
				XmlElement xmlElement = (XmlElement)xmlNode;
				if (startupType == 0 || !(xmlElement.Attributes["name"].Value != "当前样式") || !(xmlElement.Attributes["startupType"].Value != startupType.ToString()))
				{
					bigStruct.style = xmlElement.Attributes["name"].Value;
					List<systemTypeStruct> list2 = new List<systemTypeStruct>();
					systemTypeStruct[] array = XML.readstruct(path, rootNodeName, bigStruct.style, "空调风系统");
					if (array != null)
					{
						list2.AddRange(array);
					}
					array = XML.readstruct(path, rootNodeName, bigStruct.style, "风管系统", startupType);
					if (array != null)
					{
						list2.AddRange(array);
					}
					bigStruct.DuctSystemStruct = list2.ToArray();
					list2.Clear();
					array = XML.readstruct(path, rootNodeName, bigStruct.style, "空调水系统");
					if (array != null)
					{
						list2.AddRange(array);
					}
					array = XML.readstruct(path, rootNodeName, bigStruct.style, "给排水系统");
					if (array != null)
					{
						list2.AddRange(array);
					}
					array = XML.readstruct(path, rootNodeName, bigStruct.style, "消防系统");
					if (array != null)
					{
						list2.AddRange(array);
					}
					array = XML.readstruct(path, rootNodeName, bigStruct.style, "采暖系统");
					if (array != null)
					{
						list2.AddRange(array);
					}
					array = XML.readstruct(path, rootNodeName, bigStruct.style, "其他");
					if (array != null)
					{
						list2.AddRange(array);
					}
					array = XML.readstruct(path, rootNodeName, bigStruct.style, "管道系统", startupType);
					if (array != null)
					{
						list2.AddRange(array);
					}
					bigStruct.PipeSystemStruct = list2.ToArray();
					list.Add(bigStruct);
				}
			}
			return list;
		}

		public static systemTypeStruct[] readstruct(string path, string rootNodeName, string styleNodeName, string readNodeName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode(rootNodeName).ChildNodes;
			XmlNodeList xmlNodeList = null;
			XmlNodeList xmlNodeList2 = null;
			foreach (object obj in childNodes)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (xmlElement.Attributes["name"].Value == styleNodeName)
				{
					xmlNodeList = xmlElement.ChildNodes;
					break;
				}
			}
			foreach (object obj2 in xmlNodeList)
			{
				XmlElement xmlElement2 = (XmlElement)((XmlNode)obj2);
				if (xmlElement2.Attributes["name"].Value == readNodeName)
				{
					xmlNodeList2 = xmlElement2.ChildNodes;
					break;
				}
			}
			if (xmlNodeList2 == null)
			{
				return null;
			}
			int count = xmlNodeList2.Count;
			systemTypeStruct[] array = new systemTypeStruct[count];
			for (int i = 0; i < count; i++)
			{
				array[i] = new systemTypeStruct();
			}
			int num = 0;
			if (readNodeName == "空调风系统" || readNodeName == "风管系统")
			{
				readNodeName = "风管系统";
			}
			else
			{
				readNodeName = "管道系统";
			}
			foreach (object obj3 in xmlNodeList2)
			{
				XmlElement xmlElement3 = (XmlElement)((XmlNode)obj3);
				if (num < count)
				{
					array[num].SystemClass = readNodeName;
					array[num].PipingSystemType = xmlElement3.Attributes["pipingSystemType"].Value;
					array[num].PipeSystemType = xmlElement3.Attributes["PipeSystemType"].Value;
					array[num].PipingSystemTypeAbb = xmlElement3.Attributes["PipingSystemTypeAbb"].Value;
					array[num].Color = xmlElement3.Attributes["color"].Value;
					array[num].LinePattern = xmlElement3.Attributes["linePattern"].Value;
					array[num].LineWeight = xmlElement3.Attributes["lineWeight"].Value;
					num++;
				}
			}
			return array;
		}

		public static List<string> GetSystemClass(string path, string rootNodeName, string styleNodeName, string readNodeName)
		{
			List<string> list = new List<string>();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			foreach (object obj in xmlDocument.SelectSingleNode(rootNodeName).ChildNodes)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (xmlElement.Attributes["name"].Value == readNodeName)
				{
					foreach (object obj2 in xmlElement.ChildNodes)
					{
						XmlElement xmlElement2 = (XmlElement)((XmlNode)obj2);
						list.Add(xmlElement2.Attributes["pipingSystemType"].Value.ToString());
					}
				}
			}
			return list;
		}

		public static void deleteNext(string path, string rootNodeName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			xmlDocument.SelectSingleNode(rootNodeName).RemoveAll();
			xmlDocument.Save(path);
		}

		public static void writeInfoToXML(string path, string styleNodeName, string rootNodeName, List<systemTypeStruct> list)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNode xmlNode = xmlDocument.SelectSingleNode(rootNodeName);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode(rootNodeName).ChildNodes;
			if (!false)
			{
				XmlNode xmlNode2 = xmlDocument.CreateElement("styleNode");
				(xmlNode2 as XmlElement).SetAttribute("name", styleNodeName);
				xmlNode.AppendChild(xmlNode2);
				XmlNode xmlNode3 = xmlDocument.CreateElement("System");
				(xmlNode3 as XmlElement).SetAttribute("name", "风管系统");
				xmlNode2.AppendChild(xmlNode3);
				XmlNode xmlNode4 = xmlDocument.CreateElement("System");
				(xmlNode4 as XmlElement).SetAttribute("name", "管道系统");
				xmlNode2.AppendChild(xmlNode4);
				foreach (string text in new List<string>
				{
					"风管系统",
					"管道系统"
				})
				{
					foreach (systemTypeStruct systemTypeStruct in list)
					{
						if (systemTypeStruct.SystemClass == text)
						{
							XmlNode xmlNode5 = xmlDocument.CreateElement("add");
							(xmlNode5 as XmlElement).SetAttribute("pipingSystemType", systemTypeStruct.PipingSystemType);
							(xmlNode5 as XmlElement).SetAttribute("PipingSystemTypeAbb", systemTypeStruct.PipingSystemTypeAbb);
							(xmlNode5 as XmlElement).SetAttribute("PipeSystemType", systemTypeStruct.PipeSystemType);
							(xmlNode5 as XmlElement).SetAttribute("color", systemTypeStruct.Color);
							(xmlNode5 as XmlElement).SetAttribute("linePattern", systemTypeStruct.LinePattern);
							(xmlNode5 as XmlElement).SetAttribute("lineWeight", systemTypeStruct.LineWeight);
							if (text == "风管系统")
							{
								xmlNode3.AppendChild(xmlNode5);
							}
							else if (text == "管道系统")
							{
								xmlNode4.AppendChild(xmlNode5);
							}
						}
					}
				}
				xmlDocument.Save(path);
			}
		}

		public static void writeInfoToXML(string path, string styleNodeName, string rootNodeName, List<systemTypeStruct> list, int startupType)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNode xmlNode = xmlDocument.SelectSingleNode(rootNodeName);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode(rootNodeName).ChildNodes;
			if (!false)
			{
				XmlNode xmlNode2 = xmlDocument.CreateElement("styleNode");
				(xmlNode2 as XmlElement).SetAttribute("name", styleNodeName);
				if (styleNodeName != "当前样式")
				{
					(xmlNode2 as XmlElement).SetAttribute("startupType", startupType.ToString());
				}
				xmlNode.AppendChild(xmlNode2);
				XmlNode xmlNode3 = xmlDocument.CreateElement("System");
				(xmlNode3 as XmlElement).SetAttribute("name", "风管系统");
				if (styleNodeName != "当前样式")
				{
					(xmlNode3 as XmlElement).SetAttribute("startupType", startupType.ToString());
				}
				xmlNode2.AppendChild(xmlNode3);
				XmlNode xmlNode4 = xmlDocument.CreateElement("System");
				(xmlNode4 as XmlElement).SetAttribute("name", "管道系统");
				if (styleNodeName != "当前样式")
				{
					(xmlNode4 as XmlElement).SetAttribute("startupType", startupType.ToString());
				}
				xmlNode2.AppendChild(xmlNode4);
				foreach (string text in new List<string>
				{
					"风管系统",
					"管道系统"
				})
				{
					foreach (systemTypeStruct systemTypeStruct in list)
					{
						if (systemTypeStruct.SystemClass == text)
						{
							XmlNode xmlNode5 = xmlDocument.CreateElement("add");
							(xmlNode5 as XmlElement).SetAttribute("pipingSystemType", systemTypeStruct.PipingSystemType);
							(xmlNode5 as XmlElement).SetAttribute("PipingSystemTypeAbb", systemTypeStruct.PipingSystemTypeAbb);
							(xmlNode5 as XmlElement).SetAttribute("PipeSystemType", systemTypeStruct.PipeSystemType);
							(xmlNode5 as XmlElement).SetAttribute("color", systemTypeStruct.Color);
							(xmlNode5 as XmlElement).SetAttribute("linePattern", systemTypeStruct.LinePattern);
							(xmlNode5 as XmlElement).SetAttribute("lineWeight", systemTypeStruct.LineWeight);
							if (text == "风管系统")
							{
								xmlNode3.AppendChild(xmlNode5);
							}
							else if (text == "管道系统")
							{
								xmlNode4.AppendChild(xmlNode5);
							}
						}
					}
				}
				xmlDocument.Save(path);
			}
		}

		public static void deleteNext(string path, string rootNodeName, int startupType, string selectedNode = "")
		{
			XmlDocument xmlDocument = new XmlDocument();
			if (!File.Exists(path))
			{
				XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
				xmlDocument.AppendChild(newChild);
				XmlElement newChild2 = xmlDocument.CreateElement("systemTypeManage");
				xmlDocument.AppendChild(newChild2);
				xmlDocument.Save(path);
			}
			xmlDocument.Load(path);
			XmlNode xmlNode = null;
			List<XmlNode> list = new List<XmlNode>();
			foreach (object obj in xmlDocument.SelectSingleNode("systemTypeManage").ChildNodes)
			{
				XmlNode xmlNode2 = (XmlNode)obj;
				if (xmlNode2.Attributes["name"].Value != "当前样式")
				{
					if (xmlNode2.Attributes["startupType"].Value == startupType.ToString())
					{
						list.Add(xmlNode2);
					}
				}
				else
				{
					xmlNode = xmlNode2;
				}
			}
			if (xmlNode != null && selectedNode == "")
			{
				xmlNode.ParentNode.RemoveChild(xmlNode);
			}
			foreach (XmlNode xmlNode3 in list)
			{
				if (selectedNode != "" && xmlNode3.Attributes["name"].Value == selectedNode)
				{
					xmlNode3.ParentNode.RemoveChild(xmlNode3);
				}
				else if (selectedNode == "")
				{
					xmlNode3.ParentNode.RemoveChild(xmlNode3);
				}
			}
			xmlDocument.Save(path);
		}

		public static systemTypeStruct[] readstruct(string path, string rootNodeName, string styleNodeName, string readNodeName, int startupType = 0)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode(rootNodeName).ChildNodes;
			XmlNodeList xmlNodeList = null;
			XmlNodeList xmlNodeList2 = null;
			foreach (object obj in childNodes)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (styleNodeName != "当前样式")
				{
					if (startupType != 0 && xmlElement.Attributes["name"].Value == styleNodeName && xmlElement.Attributes["startupType"].Value == startupType.ToString())
					{
						xmlNodeList = xmlElement.ChildNodes;
						break;
					}
				}
				else if (xmlElement.Attributes["name"].Value == styleNodeName)
				{
					xmlNodeList = xmlElement.ChildNodes;
					break;
				}
			}
			if (xmlNodeList == null)
			{
				return null;
			}
			foreach (object obj2 in xmlNodeList)
			{
				XmlElement xmlElement2 = (XmlElement)((XmlNode)obj2);
				if (xmlElement2.Attributes["name"].Value == readNodeName)
				{
					xmlNodeList2 = xmlElement2.ChildNodes;
					break;
				}
			}
			if (xmlNodeList2 == null)
			{
				return null;
			}
			int count = xmlNodeList2.Count;
			systemTypeStruct[] array = new systemTypeStruct[count];
			for (int i = 0; i < count; i++)
			{
				array[i] = new systemTypeStruct();
			}
			int num = 0;
			if (readNodeName == "空调风系统" || readNodeName == "风管系统")
			{
				readNodeName = "风管系统";
			}
			else
			{
				readNodeName = "管道系统";
			}
			foreach (object obj3 in xmlNodeList2)
			{
				XmlElement xmlElement3 = (XmlElement)((XmlNode)obj3);
				if (num < count)
				{
					array[num].SystemClass = readNodeName;
					array[num].PipingSystemType = xmlElement3.Attributes["pipingSystemType"].Value;
					array[num].PipeSystemType = xmlElement3.Attributes["PipeSystemType"].Value;
					array[num].PipingSystemTypeAbb = xmlElement3.Attributes["PipingSystemTypeAbb"].Value;
					array[num].Color = xmlElement3.Attributes["color"].Value;
					array[num].LinePattern = xmlElement3.Attributes["linePattern"].Value;
					array[num].LineWeight = xmlElement3.Attributes["lineWeight"].Value;
					num++;
				}
			}
			return array;
		}
	}
}
